#include "MiniParaCurveHandler.h"

MiniParaCurveHandler::MiniParaCurveHandler(MiniObject *obj)
    : MiniHandler(obj)
{
    UpdateHandler();
}

float MiniParaCurveHandler::CheckCollision(QPointF pos, QMatrix4x4 projection)
{
    // 之后增加选中程序
    Q_UNUSED(pos)
    Q_UNUSED(projection)
    return 1;
}

void MiniParaCurveHandler::ChangeVertex(Vector3f dv)
{
    Q_UNUSED(dv)
}

void MiniParaCurveHandler::UpdateHandler()
{
    InitHandler();

    // 转化为分段直线
    MiniParaCurve *curve = (MiniParaCurve*)m_object;
    Real bu = curve->GetBegin();
    Real eu = curve->GetEnd();
    float* vertices = new float[1000 * 3];
    unsigned int* indices = new unsigned int[1000];
    for (int i = 0; i < 1000; i++)
    {
        indices[i] = i;
        Vector3f p = curve->CurvePoint(bu + (eu - bu) / 999 * i);
        vertices[i * 3 + 0] = p.x();
        vertices[i * 3 + 1] = p.y();
        vertices[i * 3 + 2] = p.z();
    }

    // 储存数据
    m_countIndices.push_back(1000);
    m_countVertices.push_back(1000);
    m_vertices.push_back(vertices);
    m_indices.push_back(indices);
    m_modes.push_back(GL_LINE_STRIP);
    m_color.push_back(m_basisColor);
    m_lineWidth.push_back(2);
}
